public class TestHeap {

    public int[] elem;
    public int useSize;

    public TestHeap() {
        elem = new int[10];
    }

    public void initHeap(int[] array) {
        for (int i = 0;i < array.length;i++) {
            elem[i] = array[i];
            useSize++;
        }
    }
    public void creatHeap() {
        for (int parent = (useSize - 1 - 1) / 2; parent >= 0; parent--) {
            siftDown(parent, useSize);
        }
    }

    public void siftDown(int parent, int useSize) {
        int child = parent * 2 + 1;
        while (child < useSize) {
            if (child + 1 < useSize && elem[child] < elem[child + 1]) {
                child++;
            }
            if (elem[parent] < elem[child]) {
                swap(elem, parent, child);
                parent = child;
                child = parent * 2 + 1;
            } else {
                break;
            }
        }
    }

    public void swap(int[] elem, int i, int j) {
        int p = elem[i];
        elem[i] = elem[j];
        elem[j] = p;
    }
}
